import threading, time

'''
# <summary>
# Processes tiles for the Main thread.
# </summary>
# <param name="request_que" type="Queue">For communication between threads</param>
'''
class LoopingThread(threading.Thread):
    
    __request_que = None
    __is_running = True
    
    def __init__(self, request_que):
        threading.Thread.__init__(self, name="LoopingThread")
        self.__request_que = request_que
            
    def run(self):
        while self.__is_running:
            if (self.__request_que.qsize() > 0):
                request = self.__request_que.get()
                
                tile_schema, extent, resolution, do, terminate = request
                if not terminate is None:
                    terminate()
                    return

                tiles = tile_schema.getTilesInView1(extent, resolution)
                do(tiles, extent, resolution)
            else:
                '''Let the tread sleep this lowers the CPU usage'''
                time.sleep(0.1)